<!doctype html>
<html>
    <head>
        <title>Exceptions</title>
    </head>
    <body>
        <?php
            /*
             * Creat function with an exception
             */
            function checkNumber($number)
            {
                if($number>1)
                {
                    throw new Exception ("Number should be less than 1");
                }
                return true;
            }
            /*
             * Trigger new exception inside a try...catch block
             */
            try
            {
                checkNumber(2);
                /*
                 * If the exception is thrown you will not see the text below
                 */
                echo "The number is less then 1";
            }
            catch(exception $e)
            {
                echo "Message: " . $e->getMessage() . "<br/>";
            }
            
            
            /*
             * Creating a Custom Exception Class
             */
            class customException extends Exception
            {
                public function errorMessage()
                {
                    $errorMsg = "Error on line " .
                            $this->getLine() . " in " . 
                            $this->getFile() . " : <b>" . 
                            $this->getMessage() .  "</b> is not a valid E-mail address <br/>";
                
                    return $errorMsg;
                }
            }
            $newEmail = "somee@mail..aaa";
            try
            {
                if(filter_var($newEmail,FILTER_VALIDATE_EMAIL) === FALSE)
                {
                    /*
                     * if the e-mail is not valid, throw new custom exception
                     */
                    throw new customException($newEmail);
                }
            }
            catch(customException $e)
            {
                echo $e->errorMessage();
            }
            
            /*
             * Multiple exceptions
             */
            $newEmail1 = "somee@example.aaa";
            try
            {
                if(filter_var($newEmail1,FILTER_VALIDATE_EMAIL) === false)
                {
                    throw new customException($newEmail1);
                }
                /*
                 * If the e-mail contains the word "mail", throw new exception
                 */
                if(strpos($newEmail1,'example') !== false)
                {
                    throw new Exception("$newEmail1 is an example email<br/>");
                }
            }
            catch(customException $e)
            {
                echo $e->errorMessage();
            }
            catch(Exception $e)
            {
                echo $e->getMessage();
            }
            
            /*
             * Rethrowing exceptions
             */
            try
            {
                try
                {
                    /*
                     * Check if "example" is part of the email name
                     * if so, throw new exception
                     */
                    if(strpos($newEmail1 , "example") !== false)
                    {
                        throw new Exception($newEmail1);
                    }
                }
                catch(Exception $e)
                {
                    /*
                     * Re-throw exception
                     */
                    throw new customException($newEmail1);
                }
            }
            catch(customException $e)
            {
                echo $e->errorMessage();
            }
            
            /*
             * Set a top level exception handler
             * Using the function set_exception_handler();
             */
            function myException($exception)
            {
                echo "<b>Exception:</b> " , $exception->getMessage();
            }
            set_exception_handler('myException');
            throw new Exception('Uncaught Exception occurred');
        ?>
    </body>
</html>
